﻿#region

using System;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.POI;
using Styx.WoWInternals;

#endregion

namespace MIBHunter
{
    public static class EventHandlers
    {
        public static bool CombatLogAttached;

        public static void AttachCombatLogEvent()
        {
            //Don't attach log in in bg, dungeon, raid, that slow the CC
            if (StyxWoW.Me.IsInInstance)
                return;

            if (CombatLogAttached)
                return;

            // DO NOT EDIT THIS UNLESS YOU KNOW WHAT YOU'RE DOING!
            // This ensures we only capture certain combat log events, not all of them.
            // This saves on performance, and possible memory leaks. (Leaks due to Lua table issues.)
            Lua.Events.AttachEvent("COMBAT_LOG_EVENT_UNFILTERED", HandleCombatLog);
            if (
                !Lua.Events.AddFilter(
                    "COMBAT_LOG_EVENT_UNFILTERED",
                     "return args[2] == 'SWING_MISSED'"))
            {
            }

            CombatLogAttached = true;
        }

        public static void DetachCombatLogEvent()
        {
            if (!CombatLogAttached)
                return;

            Lua.Events.DetachEvent("COMBAT_LOG_EVENT_UNFILTERED", HandleCombatLog);
            CombatLogAttached = false;
        }

        public static void HandleCombatLog(object sender, LuaEventArgs args)
        {
            var e = new CombatLogEventArgs(args.EventName, args.FireTimeStamp, args.Args);
            switch (e.Event)
            {
                case "SWING_MISSED":
                    if (e.Args[11].ToString() == "EVADE")
                    {
                        Logging.Write("Mob is evading swing. Blacklisting it!");
                        Blacklist.Add(e.DestGuid,BlacklistFlags.All, TimeSpan.FromMinutes(5));
                        if (StyxWoW.Me.CurrentTargetGuid == e.DestGuid)
                        {
                            StyxWoW.Me.ClearTarget();
                        }

                        BotPoi.Clear("Blacklisting evading mob");
                        StyxWoW.SleepForLagDuration();
                    }
                    break;

                case "RANGE_MISSED":
                    if (e.Args[14].ToString() == "EVADE")
                    {
                        Logging.Write("Mob is evading ranged attack. Blacklisting it!");
                        Blacklist.Add(e.DestGuid, BlacklistFlags.All, TimeSpan.FromMinutes(30));
                        if (StyxWoW.Me.CurrentTargetGuid == e.DestGuid)
                        {
                            StyxWoW.Me.ClearTarget();
                        }

                        BotPoi.Clear("Blacklisting evading mob");
                        StyxWoW.SleepForLagDuration();
                    }
                    break;
            }
        }
    }
}